package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.packers.EngineeringCompanyEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liwei
 * @date 2021/3/24
 * description:
 */
public class PackersEngineeringCompanyModel extends AndroidViewModel {

    private PickSelectEntity all;

    private int page = 1;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<EngineeringCompanyEntity>> companyList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> popList = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> cityList = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity>>>> dataList = new MutableLiveData<>();

    public PackersEngineeringCompanyModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getNewList(String categoryNo, String secondCategoryNo, String thirdCategoryNo, String teamSize, String provinceNo, String cityNo, String areaNo, boolean b,String qualificationTypeNo,String qualificationClassNo) {
        Map<String, Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        int limit = 10;
        map.put("limit", limit);
        map.put("page", page);
        if (!TextUtils.isEmpty(categoryNo)) {
            map.put("categoryNo", categoryNo);
        }

        if (!TextUtils.isEmpty(secondCategoryNo)) {
            map.put("secondCategoryNo", secondCategoryNo);
        }

        if (!TextUtils.isEmpty(thirdCategoryNo)) {
            map.put("thirdCategoryNo", thirdCategoryNo);
        }

        if (!TextUtils.isEmpty(teamSize) && !TextUtils.equals("全部", teamSize)) {
            map.put("teamNumber", teamSize);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
            if (!TextUtils.isEmpty(areaNo)) {
                List<String> stringList = new ArrayList<>();
                stringList.add(areaNo);
                map.put("areaNoList", stringList);
            }
        } else {
            if (!TextUtils.isEmpty(areaNo)) {
                List<String> stringList = new ArrayList<>();
                stringList.add(areaNo);
                map.put("areaNoList", stringList);
            }
        }

        if (!TextUtils.isEmpty(qualificationTypeNo)){
            map.put("qualificationTypeNo", qualificationTypeNo);
        }

        if (!TextUtils.isEmpty(qualificationClassNo)){
            map.put("qualificationClassNo", qualificationClassNo);
        }
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEngineeringList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<EngineeringCompanyEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() != null) {
                    companyList.postValue(data.getData().getList());
                }
            }
        });
    }

    /**
     * 团队规模
     *
     * @param type
     */
    public void getTeamSize(int type) {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamScale("" + type).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data1) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> data = data1.getData();
                Collections.reverse(data);
                data.add(0, all);
                popList.postValue(data);
            }
        });
    }

    /**
     * 省市区 城市列表
     */
    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                cityList.postValue(data.getData());
            }
        });
    }

    public void getIntelligenceType() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().intelligenceType().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.isSuccess()){
                    data.getData().add(0,new AddressEntity<>("","全部",""));
                    for (AddressEntity<List<AddressEntity>> datum : data.getData()) {
                        if (datum.getChildren() == null) {
                            datum.setChildren(new ArrayList<>());
                        }
                        datum.getChildren().add(0,new AddressEntity<>("","全部",""));
                    }
                    dataList.postValue(data.getData());
                }
            }
        });
    }
}
